package com.zhangsun.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhangsun.base.entity.BaseEntity;
import com.zhangsun.expection.BusinessException;
import com.zhangsun.system.entity.DictEntity;
import com.zhangsun.system.entity.DictTypeEntity;
import com.zhangsun.system.mapper.DictMapper;
import com.zhangsun.system.mapper.DictTypeMapper;
import com.zhangsun.system.service.DictTypeService;
import com.zhangsun.system.vo.dict.DictTypeEditVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 数据字典类型 服务实现类
 * </p>
 *
 * @author zhangsun
 * @since 2019-08-13
 */
@Service
public class DictTypeServiceImpl extends ServiceImpl<DictTypeMapper, DictTypeEntity> implements DictTypeService {

    @Autowired
    private DictMapper dictMapper;

    @Override
    public List<DictTypeEntity> findAll() {
        LambdaQueryWrapper<DictTypeEntity> queryWrapper = new QueryWrapper<DictTypeEntity>().lambda();
        queryWrapper.orderByAsc(DictTypeEntity::getSort);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public void create(DictTypeEditVo vo) {
        checkTypeCode(vo.getTypeCode(), null);

        DictTypeEntity dictTypeEntity = new DictTypeEntity();
        BeanUtil.copyProperties(vo, dictTypeEntity);
        dictTypeEntity.insert();
    }

    @Override
    public DictTypeEntity findById(String id) {
        return baseMapper.selectById(id);
    }

    @Override
    public void update(String id, DictTypeEditVo vo) {
        checkTypeCode(vo.getTypeCode(), id);

        DictTypeEntity dictTypeEntity = new DictTypeEntity();
        BeanUtil.copyProperties(vo, dictTypeEntity);
        dictTypeEntity.setId(id);
        dictTypeEntity.updateById();
    }

    private void checkTypeCode(String typeCode, String id) {
        LambdaQueryWrapper<DictTypeEntity> queryWrapper = new QueryWrapper<DictTypeEntity>().lambda();
        queryWrapper.eq(DictTypeEntity::getTypeCode, typeCode);
        if (StrUtil.isNotBlank(id)) {
            queryWrapper.ne(DictTypeEntity::getId, id);
        }
        int count = baseMapper.selectCount(queryWrapper);
        if (count > 0) {
            throw new BusinessException("该分类代码已存在");
        }
    }

    @Transactional
    @Override
    public void delete(String id) {
        DictTypeEntity dictTypeEntity = baseMapper.selectById(id);
        if (dictTypeEntity != null) {
            baseMapper.deleteById(id);

            LambdaQueryWrapper<DictEntity> queryWrapper = new QueryWrapper<DictEntity>().lambda();
            queryWrapper.eq(DictEntity::getTypeCode, dictTypeEntity.getTypeCode());
            dictMapper.delete(queryWrapper);
        }
    }
}
